package com.tinyengine.it.controller.db;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.entity.DbDictInfo;
import com.tinyengine.it.model.entity.DbDictionaryInfo;
import com.tinyengine.it.model.entity.DbTableFieldsInfo;
import com.tinyengine.it.model.vo.DbDictInfoVo;
import com.tinyengine.it.model.vo.DbDictionaryInfoVo;
import com.tinyengine.it.service.db.DbDictInfoService;
import com.tinyengine.it.service.db.DbDictionaryInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/dict-dict/api")
@Tag(name = "字典信息维护")
@Validated
@RequiredArgsConstructor
public class DbDictInfoController {

    private final DbDictionaryInfoService dbDictionaryInfoService;
    private final DbDictInfoService dbDictInfoService;

    @Operation(summary = "跟表ID获取条目列表",
            parameters = {
                    @Parameter(name = "dictId", description = "ID")
            },
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = DbTableFieldsInfo.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/getDictInfoByDictIdId")
    public Result<List<DbDictionaryInfoVo>> getDictInfoByDictIdId(@RequestParam("dictId") Integer dictId) {
        List<DbDictionaryInfo> dictionaryInfos = dbDictionaryInfoService.list(Wrappers.<DbDictionaryInfo>lambdaQuery().eq(DbDictionaryInfo::getParentId, dictId));
        return Result.success(BeanUtil.copyToList(dictionaryInfos, DbDictionaryInfoVo.class));
    }

    @Operation(summary = "获取所有字典代码信息",
            parameters = {
            },
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = DbTableFieldsInfo.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/getDictList")
    public Result<List<DbDictInfoVo>> getDictList() {
        List<DbDictInfo> list = dbDictInfoService.list();
        List<DbDictInfoVo> dbDictInfoVos = BeanUtil.copyToList(list, DbDictInfoVo.class);
        return Result.success(dbDictInfoVos);
    }

    @Operation(summary = "新增及更新包含条目",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(@RequestBody @Validated DbDictInfoVo dbDictInfoVo) {

        DbDictInfo dictInfo = dbDictInfoService.getOne(Wrappers.<DbDictInfo>lambdaQuery().eq(DbDictInfo::getCode, dbDictInfoVo.getCode()));
        if (ObjUtil.isNull(dbDictInfoVo.getId()) && ObjUtil.isNotNull(dictInfo)) {
            throw new RuntimeException("字典Code已经存在，请修改!");
        }
        return Result.success(dbDictInfoService.saveInfo(dbDictInfoVo));
    }


    @Operation(summary = "根据字典ID删除 整个字段，包含条目",
            parameters = {
                    @Parameter(name = "id", description = "ID")},
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@Valid @PathVariable Integer id) {
        dbDictInfoService.removeById(id);
        dbDictionaryInfoService.remove(Wrappers.<DbDictionaryInfo>lambdaQuery().eq(DbDictionaryInfo::getParentId, id));
        return Result.success();
    }

    @Operation(summary = "根据字典code获取条目",
            parameters = {
                    @Parameter(name = "code", description = "code")},
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/getByCode/{code}")
    public Result getDictByCode(@Valid @PathVariable String code) {
        DbDictInfo dictInfo = dbDictInfoService.getOne(Wrappers.<DbDictInfo>lambdaQuery().eq(DbDictInfo::getCode, code));
        if (ObjUtil.isNull(dictInfo)) {
            return Result.success(Collections.EMPTY_LIST);
        }
        List<DbDictionaryInfo> dictionaryInfos = dbDictionaryInfoService.list(Wrappers.<DbDictionaryInfo>lambdaQuery().eq(DbDictionaryInfo::getParentId, dictInfo.getId()));
        List<DbDictionaryInfoVo> dbDictionaryInfoVos = BeanUtil.copyToList(dictionaryInfos, DbDictionaryInfoVo.class);
        return Result.success(dbDictionaryInfoVos);
    }
}
